<?php
/**
 * Created by PhpStorm.
 * User: hoter.zhang
 * Date: 2016/3/11
 * Time: 9:30
 */

namespace app\modules\admin\models;


use app\models\Admin;
use yii\base\Model;

class SignupForm extends Model
{

    public $username;
    public $password;
    public $verifyPassword;

    public function rules()
    {
        return [
            ['username', 'filter', 'filter' => 'trim'],
            ['username', 'required'],
            ['username', 'unique', 'targetClass' => '\app\models\Admin', 'message' => 'This username has already been taken.'],
            ['username', 'string', 'min' => 2, 'max' => 255],

            ['password', 'required'],
            ['password', 'string', 'min' => 6],

            ['verifyPassword', 'required'],
            ['verifyPassword', 'string', 'min' => 6],
            ['verifyPassword','compare', 'compareAttribute' => 'password'],
        ];
    }

    public function signup(){
        if(!$this->validate()) {
            print_r($this->getFirstErrors());
            return null;
        }
        $user = new Admin();
        $user->username = $this->username;
        $user->setPassword($this->password);
        $user->generateAuthKey();
        $user->status = 0;
        $user->nick_name = time();
//        return $shop->save() ? $shop : null;
        if($user->save(false)){
            return $user;
        } else {
            print_r($user->getFirstErrors());die;
        }
    }
}